package io.yizoo.system.mapper;

import io.elm.core.Ctor;
import io.elm.core.Dtor;

import java.util.HashMap;
import java.util.Map;

public class  sys_menuMapperProvider {
    public static String SelectUserMenuTree(Map<String,Object> factor) {
        String sql = Dtor.getSql("WITH RECURSIVE sys_menu_tree AS (\n" +
                "            SELECT\n" +
                "                 t.*,\n" +
                "                0 AS level,\n" +
                "                CONCAT(LPAD(CAST(t.sort AS CHAR), 3, '0'),'_',CAST(t.id AS CHAR(1000))) AS path\n" +
                "            FROM\n" +
                "                sys_menu t\n" +
                "left join sys_role_rel_menu a on a.menu_id=t.id\n" +
                "left join sys_role_rel_user b on b.role_id=a.role_id\n" +
                "            WHERE\n" +
                "                b.user_id=#{user_id} and  (t.client=#{client} or t.client is null or t.client='电脑及移动端')\n" +
                "\t\t\t\t\t\t\n" +
                "            UNION ALL\n" +
                " \n" +
                "            SELECT\n" +
                "                c. *,\n" +
                "                ct.level + 1 AS level,\n" +
                "                CONCAT(ct.path, '_',LPAD(CAST(c.sort AS CHAR), 3, '0'),'_',c.id) AS path\n" +
                "            FROM\n" +
                "                sys_menu c\n" +
                "            INNER JOIN\n" +
                "                sys_menu_tree ct ON c.id = ct.pid\n" +
                "               where (c.client=#{client} or c.client is null  or c.client='电脑及移动端')" +
                "\n" +
                "        )\n" +
                "        SELECT\n" +
                "             distinct id,pid,name,icon,navigateurl,sort,target,menu_display\n" +
                "        FROM\n" +
                "            sys_menu_tree\n" +
                "        ORDER BY\n" +
                "            path\n" +
                "\t\t\t\t\t\t",factor);
        return sql;
    }

    public static void main(String[] args) {

        Ctor ctor = new Ctor();
        Map<String, Object> map = ctor.DMaker().Selector.putFactorItem("user_id", "22222222222222").GetFactor();
        String s = SelectUserMenuTree(map);
        System.out.println(s);
    }
}
